﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using HYCodeBase.XmlUtility;

namespace HYElectricSetting
{
	public partial class CableDataImportForm : System.Windows.Forms.Form
	{
		public CableDataImportForm()
		{
			this.InitializeComponent();
			this.m_Attributes = new Dictionary<CableField, string>();
		}

		private void CableDataImportForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.cmbExcel.Visible = false;
			this.dataGridView.Controls.Add(this.cmbExcel);
			this.ReadFormDataFromXml();
			this.UpdateDataGridViewData(true);
		}

		private void ReadFormDataFromXml()
		{
			string electricTempFile = CableManageCommon.GetElectricTempFile();
			HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(electricTempFile, "ElectricData");
			xmlHelper.SelectNode("ImportCableFromExcel");
			string text = Path.Combine(Product.InstallLocation, "UserWork");
			string attributeEx = xmlHelper.GetAttributeEx("ExcelPath", text);
			this.textBoxExcelFile.Text = (string.IsNullOrWhiteSpace(attributeEx) ? text : attributeEx);
			attributeEx = xmlHelper.GetAttributeEx("StartRow", "2");
			this.txtStartRow.Text = attributeEx;
			attributeEx = xmlHelper.GetAttributeEx("CableName", "A");
			this.m_Attributes.Add(CableField.Field_CableName, attributeEx);
			attributeEx = xmlHelper.GetAttributeEx("CableModel", "B");
			this.m_Attributes.Add(CableField.Field_CableModel, attributeEx);
			attributeEx = xmlHelper.GetAttributeEx("CableSpec", "C");
			this.m_Attributes.Add(CableField.Field_CableSpec, attributeEx);
			attributeEx = xmlHelper.GetAttributeEx("SystemType", "D");
			this.m_Attributes.Add(CableField.Field_SystemType, attributeEx);
			attributeEx = xmlHelper.GetAttributeEx("OutDiameter", "E");
			this.m_Attributes.Add(CableField.Field_OutDiameter, attributeEx);
		}

		private void SaveFormDataToXml()
		{
			string electricTempFile = CableManageCommon.GetElectricTempFile();
			HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(electricTempFile, "ElectricData");
			xmlHelper.SelectNode("ImportCableFromExcel");
			xmlHelper.SetAttribute("ExcelPath", this.textBoxExcelFile.Text.Trim());
			xmlHelper.SetAttribute("StartRow", this.txtStartRow.Text.Trim());
			xmlHelper.SetAttribute("CableName", this.m_Attributes[CableField.Field_CableName]);
			xmlHelper.SetAttribute("CableModel", this.m_Attributes[CableField.Field_CableModel]);
			xmlHelper.SetAttribute("CableSpec", this.m_Attributes[CableField.Field_CableSpec]);
			xmlHelper.SetAttribute("SystemType", this.m_Attributes[CableField.Field_SystemType]);
			xmlHelper.SetAttribute("OutDiameter", this.m_Attributes[CableField.Field_OutDiameter]);
			xmlHelper.SaveXml("");
		}

		private string TitleOfField(CableField field)
		{
			string result;
			switch (field)
			{
			case CableField.Field_CableName:
				result = "电缆名称";
				break;
			case CableField.Field_CableModel:
				result = "电缆型号";
				break;
			case CableField.Field_CableSpec:
				result = "电缆规格";
				break;
			case CableField.Field_SystemType:
				result = "系统类型";
				break;
			case CableField.Field_OutDiameter:
				result = "外径(mm)";
				break;
			default:
				result = string.Empty;
				break;
			}
			return result;
		}

		private void UpdateDataGridViewData(bool flag)
		{
			if (flag)
			{
				this.dataGridView.Rows.Clear();
				foreach (KeyValuePair<CableField, string> keyValuePair in this.m_Attributes)
				{
					DataGridViewRow dataGridViewRow = new DataGridViewRow();
					int index = this.dataGridView.Rows.Add(dataGridViewRow);
					dataGridViewRow = this.dataGridView.Rows[index];
					dataGridViewRow.Cells[0].Value = this.TitleOfField(keyValuePair.Key);
					dataGridViewRow.Cells[1].Value = keyValuePair.Value;
					dataGridViewRow.Tag = keyValuePair;
				}
			}
			else
			{
				for (int i = 0; i < this.dataGridView.Rows.Count; i++)
				{
					DataGridViewRow dataGridViewRow2 = this.dataGridView.Rows[i];
					KeyValuePair<CableField, string> keyValuePair2 = (KeyValuePair<CableField, string>)dataGridViewRow2.Tag;
					this.m_Attributes[keyValuePair2.Key] = dataGridViewRow2.Cells[1].Value.ToString();
				}
			}
		}

		private void buttonBrowse_Click(object sender, EventArgs e)
		{
			try
			{
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
				openFileDialog.InitialDirectory = Path.GetDirectoryName(this.textBoxExcelFile.Text.Trim());
				bool flag = openFileDialog.ShowDialog(this) == DialogResult.OK;
				if (flag)
				{
					this.textBoxExcelFile.Text = openFileDialog.FileName;
				}
			}
			catch (Exception)
			{
				MessageBox.Show("获取Excel文件路径出错！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
		}

		private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			bool flag = e.RowIndex < 0 || e.RowIndex >= this.dataGridView.Rows.Count;
			if (!flag)
			{
				bool flag2 = e.ColumnIndex == 0;
				if (flag2)
				{
					this.cmbExcel.Visible = false;
				}
				else
				{
					this.m_rowIndex = e.RowIndex;
					this.m_columnIndex = e.ColumnIndex;
					System.Drawing.Rectangle cellDisplayRectangle = this.dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
					this.cmbExcel.Left = cellDisplayRectangle.Left;
					this.cmbExcel.Top = cellDisplayRectangle.Top;
					this.cmbExcel.Width = cellDisplayRectangle.Width;
					this.cmbExcel.Height = cellDisplayRectangle.Height;
					this.cmbExcel.Text = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
					this.cmbExcel.Visible = true;
				}
			}
		}

		private void buttonImport_Click(object sender, EventArgs e)
		{
			this.m_ExcelFile = this.textBoxExcelFile.Text.Trim();
			this.m_StartRow = Convert.ToInt32(this.txtStartRow.Text.Trim());
			this.UpdateDataGridViewData(false);
			bool flag = !File.Exists(this.m_ExcelFile);
			if (flag)
			{
				MessageBox.Show("Excel文件不存在！", "盈建科提示", MessageBoxButtons.OK);
			}
			else
			{
				bool flag2 = (double)this.m_StartRow < this.txtStartRow.MostValue.Minimal;
				if (flag2)
				{
					MessageBox.Show("起始行值设置过小，请重新设置！", "盈建科提示", MessageBoxButtons.OK);
					this.txtStartRow.Focus();
				}
				else
				{
					this.SaveFormDataToXml();
					base.Close();
					base.DialogResult = DialogResult.OK;
				}
			}
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void cmbExcel_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.dataGridView.Rows[this.m_rowIndex].Cells[this.m_columnIndex].Value = this.cmbExcel.Text;
		}

		private const string XmlNode_Root = "ImportCableFromExcel";

		private const string XmlNode_ExcelPath = "ExcelPath";

		private const string XmlNode_StartRow = "StartRow";

		private const string Item_CableName = "电缆名称";

		private const string Item_CableModel = "电缆型号";

		private const string Item_CableSpec = "电缆规格";

		private const string Item_SystemType = "系统类型";

		private const string Item_OutDiameter = "外径(mm)";

		private int m_rowIndex = 0;

		private int m_columnIndex = 1;

		public Dictionary<CableField, string> m_Attributes = null;

		public string m_ExcelFile = string.Empty;

		public int m_StartRow = 2;
	}
}
